home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / javax / swing / CellRendererPane.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  17.1 KB  |  576 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)CellRendererPane.java    1.27 98/08/28
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package javax.swing;
  15.  
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.io.*;
  19. import java.beans.PropertyChangeListener;
  20. import java.util.Locale;
  21. import java.util.Vector;
  22.  
  23. import javax.accessibility.*;
  24.  
  25. /** 
  26.  * This class is inserted in between cell renderers and the components that 
  27.  * use them.  It just exists to thwart the repaint() and invalidate() methods 
  28.  * which would otherwise propogate up the tree when the renderer was configured.
  29.  * It's used by the implementations of JTable, JTree, and JList.  For example,
  30.  * here's how CellRendererPane is used in the code the paints each row
  31.  * in a JList:
  32.  * <pre>
  33.  *   cellRendererPane = new CellRendererPane();
  34.  *   ...
  35.  *   Component rendererComponent = renderer.getListCellRendererComponent();
  36.  *   renderer.configureListCellRenderer(dataModel.getElementAt(row), row);
  37.  *   cellRendererPane.paintComponent(g, rendererComponent, this, x, y, w, h);
  38.  * </pre>
  39.  * <p>
  40.  * A renderer component must override isShowing() and unconditionally return
  41.  * true to work correctly because the Swing paint does nothing for components
  42.  * with isShowing false.  
  43.  * <p>
  44.  * <strong>Warning:</strong>
  45.  * Serialized objects of this class will not be compatible with 
  46.  * future Swing releases.  The current serialization support is appropriate
  47.  * for short term storage or RMI between applications running the same
  48.  * version of Swing.  A future release of Swing will provide support for
  49.  * long term persistence.
  50.  *
  51.  * @version 1.27 08/28/98
  52.  * @author Hans Muller
  53.  */
  54. public class CellRendererPane extends Container implements Accessible
  55. {
  56.     /**
  57.      * Construct a CellRendererPane object.
  58.      */
  59.     public CellRendererPane() {
  60.     super();
  61.     setLayout(null);
  62.     setVisible(false);
  63.     }
  64.  
  65.     /** 
  66.      * Overridden to avoid propogating a invalidate up the tree when the
  67.      * cell renderer child is configured.
  68.      */
  69.     public void invalidate() { }
  70.  
  71.  
  72.     /** 
  73.      * Shouldn't be called.
  74.      */
  75.     public void paint(Graphics g) { }
  76.  
  77.   
  78.     /**
  79.      * Shouldn't be called.
  80.      */
  81.     public void update(Graphics g) { }
  82.  
  83.  
  84.     /** 
  85.      * If the specified component is already a child of this then we don't
  86.      * bother doing anything - stacking order doesn't matter for cell
  87.      * renderer components (CellRendererPane doesn't paint anyway).<
  88.      */
  89.     protected void addImpl(Component x, Object constraints, int index) {
  90.     if (x.getParent() == this) {
  91.         return;
  92.     }
  93.     else {
  94.         super.addImpl(x, constraints, index);
  95.     }
  96.     }
  97.  
  98.  
  99.     /** 
  100.      * Paint a cell renderer component c on graphics object g.  Before the component
  101.      * is drawn it's reparented to this (if that's neccessary), it's bounds 
  102.      * are set to w,h and the graphics object is (effectively) translated to x,y.  
  103.      * If it's a JComponent, double buffering is temporarily turned off. After 
  104.      * the component is painted it's bounds are reset to -w, -h, 0, 0 so that, if 
  105.      * it's the last renderer component painted, it will not start consuming input.  
  106.      * The Container p is the component we're actually drawing on, typically it's 
  107.      * equal to this.getParent(). If shouldValidate is true the component c will be 
  108.      * validated before painted.
  109.      */
  110.     public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h, boolean shouldValidate) {
  111.     if (c == null) {
  112.         if (p != null) {
  113.         Color oldColor = g.getColor();
  114.         g.setColor(p.getBackground());
  115.         g.fillRect(x, y, w, h);
  116.         g.setColor(oldColor);
  117.         }
  118.         return;
  119.     }
  120.  
  121.     if (c.getParent() != this) {
  122.         this.add(c);
  123.     }
  124.  
  125.     c.setBounds(x, y, w, h);
  126.  
  127.     if(shouldValidate) {
  128.         c.validate();
  129.     }
  130.  
  131.     boolean wasDoubleBuffered = false;
  132.     if ((c instanceof JComponent) && ((JComponent)c).isDoubleBuffered()) {
  133.         wasDoubleBuffered = true;
  134.         ((JComponent)c).setDoubleBuffered(false);
  135.     }
  136.  
  137.     Graphics cg = SwingGraphics.createSwingGraphics(g, x, y, w, h);
  138.     try {
  139.         c.paint(cg);
  140.     }
  141.     finally {
  142.         cg.dispose();
  143.     }
  144.  
  145.     if ((c instanceof JComponent) && wasDoubleBuffered) {
  146.         ((JComponent)c).setDoubleBuffered(true);
  147.     }
  148.  
  149.     if (c instanceof JComponent) {
  150.         JComponent jc = (JComponent)c;
  151.         jc.setDoubleBuffered(wasDoubleBuffered);
  152.     }
  153.  
  154.     c.setBounds(-w, -h, 0, 0);
  155.     }
  156.  
  157.  
  158.     /**
  159.      * Calls this.paintComponent(g, c, p, x, y, w, h, false).
  160.      */
  161.     public void paintComponent(Graphics g, Component c, Container p, int x, int y, int w, int h) {
  162.     paintComponent(g, c, p, x, y, w, h, false);
  163.     }
  164.  
  165.  
  166.     /**
  167.      * Calls this.paintComponent() with the rectangles x,y,width,height fields.
  168.      */
  169.     public void paintComponent(Graphics g, Component c, Container p, Rectangle r) {
  170.     paintComponent(g, c, p, r.x, r.y, r.width, r.height);
  171.     }
  172.  
  173.  
  174.     private void writeObject(ObjectOutputStream s) throws IOException {
  175.     removeAll();
  176.     s.defaultWriteObject();
  177.     }
  178.  
  179.  
  180. /////////////////
  181. // Accessibility support
  182. ////////////////
  183.  
  184.     protected AccessibleContext accessibleContext = null;
  185.  
  186.     /**
  187.      * Get the AccessibleContext associated with this CellRendererPane
  188.      *
  189.      * @return the AccessibleContext of this CellRendererPane
  190.      */
  191.     public AccessibleContext getAccessibleContext() {
  192.     if (accessibleContext == null) {
  193.         accessibleContext = new AccessibleCellRendererPane();
  194.     }
  195.     return accessibleContext;
  196.     }
  197.  
  198.  
  199.     protected class AccessibleCellRendererPane extends AccessibleContext
  200.     implements Serializable, AccessibleComponent 
  201.     {
  202.  
  203.         // AccessibleContext methods
  204.         //
  205.         /**
  206.          * Get the role of this object.
  207.          *
  208.          * @return an instance of AccessibleRole describing the role of the 
  209.      * object
  210.          * @see AccessibleRole
  211.          */
  212.         public AccessibleRole getAccessibleRole() {
  213.         return AccessibleRole.PANEL;
  214.         }
  215.  
  216.         /**
  217.          * Get the state of this object.
  218.          *
  219.          * @return an instance of AccessibleStateSet containing the current 
  220.      * state set of the object
  221.          * @see AccessibleState
  222.          */
  223.         public AccessibleStateSet getAccessibleStateSet() {
  224.         return SwingUtilities.getAccessibleStateSet(CellRendererPane.this);
  225.         }
  226.  
  227.         /**
  228.          * Get the Accessible parent of this object.  If the parent of this
  229.          * object implements Accessible, this method should simply return
  230.          * getParent().
  231.          *
  232.          * @return the Accessible parent of this object -- can be null if this
  233.          * object does not have an Accessible parent
  234.          */
  235.         public Accessible getAccessibleParent() {
  236.         if (accessibleParent != null) {
  237.         return accessibleParent;
  238.         } else {
  239.                 Container parent = getParent();
  240.                 if (parent instanceof Accessible) {
  241.                     return (Accessible) parent;
  242.         }
  243.             }
  244.             return null;
  245.         }
  246.  
  247.         /**
  248.          * Get the index of this object in its accessible parent. 
  249.          *
  250.          * @return the index of this object in its parent; -1 if this 
  251.          * object does not have an accessible parent.
  252.          * @see #getAccessibleParent
  253.          */
  254.         public int getAccessibleIndexInParent() {
  255.         return SwingUtilities.getAccessibleIndexInParent(CellRendererPane.this);
  256.         }
  257.  
  258.         /**
  259.          * Returns the number of accessible children in the object.  If all
  260.          * of the children of this object implement Accessible, than this
  261.          * method should return the number of children of this object.
  262.          *
  263.          * @return the number of accessible children in the object.
  264.          */
  265.         public int getAccessibleChildrenCount() {
  266.         return SwingUtilities.getAccessibleChildrenCount(CellRendererPane.this);
  267.         }
  268.  
  269.         /**
  270.          * Return the nth Accessible child of the object.  
  271.          *
  272.          * @param i zero-based index of child
  273.          * @return the nth Accessible child of the object
  274.          */
  275.         public Accessible getAccessibleChild(int i) {
  276.             return SwingUtilities.getAccessibleChild(CellRendererPane.this,i);
  277.         }
  278.  
  279.         /**
  280.          * Return the locale of this object.
  281.      *
  282.          * @return the locale of this object
  283.          */
  284.         public Locale getLocale() {
  285.             return CellRendererPane.this.getLocale();
  286.         }
  287.  
  288.         /**
  289.          * Get the AccessibleComponent associated with this object if one
  290.          * exists.  Otherwise return null.
  291.          */
  292.     public AccessibleComponent getAccessibleComponent() {
  293.         return this;
  294.     }
  295.  
  296.  
  297.         // AccessibleComponent methods
  298.         //
  299.         /**
  300.          * Get the background color of this object.
  301.          *
  302.          * @return the background color, if supported, of the object; 
  303.          * otherwise, null
  304.          */
  305.         public Color getBackground() {
  306.         return CellRendererPane.this.getBackground();
  307.     }
  308.  
  309.         /**
  310.          * Set the background color of this object.
  311.          *
  312.          * @param c the new Color for the background
  313.          */
  314.         public void setBackground(Color c) {
  315.         CellRendererPane.this.setBackground(c);
  316.     }
  317.  
  318.         /**
  319.          * Get the foreground color of this object.
  320.          *
  321.          * @return the foreground color, if supported, of the object; 
  322.          * otherwise, null
  323.          */
  324.         public Color getForeground() {
  325.         return CellRendererPane.this.getForeground();
  326.     }
  327.  
  328.         /**
  329.          * Set the foreground color of this object.
  330.          *
  331.          * @param c the new Color for the foreground
  332.          */
  333.         public void setForeground(Color c) {
  334.         CellRendererPane.this.setForeground(c);
  335.     }
  336.  
  337.         /**
  338.          * Get the Cursor of this object.
  339.          *
  340.          * @return the Cursor, if supported, of the object; otherwise, null
  341.          */
  342.         public Cursor getCursor() {
  343.         return CellRendererPane.this.getCursor();
  344.     }
  345.  
  346.         /**
  347.          * Set the Cursor of this object.
  348.          *
  349.          * @param c the new Cursor for the object
  350.          */
  351.         public void setCursor(Cursor cursor) {
  352.         CellRendererPane.this.setCursor(cursor);
  353.     }
  354.  
  355.         /**
  356.          * Get the Font of this object.
  357.          *
  358.          * @return the Font,if supported, for the object; otherwise, null
  359.          */
  360.         public Font getFont() {
  361.         return CellRendererPane.this.getFont();
  362.     }
  363.  
  364.         /**
  365.          * Set the Font of this object.
  366.          *
  367.          * @param f the new Font for the object
  368.          */
  369.         public void setFont(Font f) {
  370.         CellRendererPane.this.setFont(f);
  371.     }
  372.  
  373.         /**
  374.          * Get the FontMetrics of this object.
  375.          *
  376.          * @param f the Font
  377.          * @return the FontMetrics, if supported, the object; otherwise, null
  378.          * @see getFont
  379.          */
  380.         public FontMetrics getFontMetrics(Font f) {
  381.         return CellRendererPane.this.getFontMetrics(f);
  382.     }
  383.  
  384.         /**
  385.          * Determine if the object is enabled.
  386.          *
  387.          * @return true if object is enabled; otherwise, false
  388.          */
  389.         public boolean isEnabled() {
  390.         return CellRendererPane.this.isEnabled();
  391.     }
  392.  
  393.         /**
  394.          * Set the enabled state of the object.
  395.          *
  396.          * @param b if true, enables this object; otherwise, disables it 
  397.          */
  398.         public void setEnabled(boolean b) {
  399.         CellRendererPane.this.setEnabled(b);
  400.     }
  401.     
  402.         /**
  403.          * Determine if the object is visible.  Note: this means that the
  404.          * object intends to be visible; however, it may not in fact be
  405.          * showing on the screen because one of the objects that this object
  406.          * is contained by is not visible.  To determine if an object is
  407.          * showing on the screen, use isShowing().
  408.          *
  409.          * @return true if object is visible; otherwise, false
  410.          */
  411.         public boolean isVisible() {
  412.         return CellRendererPane.this.isVisible();
  413.     }
  414.  
  415.         /**
  416.          * Set the visible state of the object.
  417.          *
  418.          * @param b if true, shows this object; otherwise, hides it 
  419.          */
  420.         public void setVisible(boolean b) {
  421.         CellRendererPane.this.setVisible(b);
  422.     }
  423.  
  424.         /**
  425.          * Determine if the object is showing.  This is determined by checking
  426.          * the visibility of the object and ancestors of the object.  Note: 
  427.      * this will return true even if the object is obscured by another 
  428.      * (for example, it happens to be underneath a menu that was pulled 
  429.      * down).
  430.          *
  431.          * @return true if object is showing; otherwise, false
  432.          */
  433.         public boolean isShowing() {
  434.         return CellRendererPane.this.isShowing();
  435.     }
  436.  
  437.         /** 
  438.          * Checks whether the specified point is within this object's bounds,
  439.          * where the point's x and y coordinates are defined to be relative to 
  440.      * the coordinate system of the object. 
  441.          *
  442.          * @param p the Point relative to the coordinate system of the object
  443.          * @return true if object contains Point; otherwise false
  444.          */
  445.         public boolean contains(Point p) {
  446.         return CellRendererPane.this.contains(p);
  447.     }
  448.     
  449.         /** 
  450.          * Returns the location of the object on the screen.
  451.          *
  452.          * @return location of object on screen -- can be null if this object
  453.          * is not on the screen
  454.          */
  455.         public Point getLocationOnScreen() {
  456.         return CellRendererPane.this.getLocationOnScreen();
  457.     }
  458.  
  459.         /** 
  460.          * Gets the location of the object relative to the parent in the form 
  461.          * of a point specifying the object's top-left corner in the screen's 
  462.          * coordinate space.
  463.          *
  464.          * @return An instance of Point representing the top-left corner of 
  465.      * the objects's bounds in the coordinate space of the screen; null if
  466.          * this object or its parent are not on the screen
  467.          */
  468.     public Point getLocation() {
  469.         return CellRendererPane.this.getLocation();
  470.     }
  471.  
  472.         /** 
  473.          * Sets the location of the object relative to the parent.
  474.          */
  475.         public void setLocation(Point p) {
  476.         CellRendererPane.this.setLocation(p);
  477.     }
  478.  
  479.         /** 
  480.          * Gets the bounds of this object in the form of a Rectangle object. 
  481.          * The bounds specify this object's width, height, and location
  482.          * relative to its parent. 
  483.          *
  484.          * @return A rectangle indicating this component's bounds; null if 
  485.      * this object is not on the screen.
  486.          */
  487.         public Rectangle getBounds() {
  488.         return CellRendererPane.this.getBounds();
  489.     }
  490.  
  491.         /** 
  492.          * Sets the bounds of this object in the form of a Rectangle object. 
  493.          * The bounds specify this object's width, height, and location
  494.          * relative to its parent.
  495.          *    
  496.          * @param A rectangle indicating this component's bounds
  497.          */
  498.         public void setBounds(Rectangle r) {
  499.         CellRendererPane.this.setBounds(r);
  500.     }
  501.  
  502.         /** 
  503.          * Returns the size of this object in the form of a Dimension object. 
  504.          * The height field of the Dimension object contains this objects's
  505.          * height, and the width field of the Dimension object contains this 
  506.      * object's width. 
  507.          *
  508.          * @return A Dimension object that indicates the size of this 
  509.      * component; null if this object is not on the screen
  510.          */
  511.         public Dimension getSize() {
  512.         return CellRendererPane.this.getSize();
  513.     }
  514.  
  515.         /** 
  516.          * Resizes this object so that it has width width and height. 
  517.          *    
  518.          * @param d - The dimension specifying the new size of the object. 
  519.          */
  520.         public void setSize(Dimension d) {
  521.         CellRendererPane.this.setSize(d);
  522.     }
  523.  
  524.         /**
  525.          * Returns the Accessible child, if one exists, contained at the local
  526.      * coordinate Point.
  527.          *
  528.          * @param p The point defining the top-left corner of the Accessible, 
  529.      * given in the coordinate space of the object's parent. 
  530.          * @return the Accessible, if it exists, at the specified location; 
  531.      * else null
  532.          */
  533.         public Accessible getAccessibleAt(Point p) {
  534.         return SwingUtilities.getAccessibleAt(CellRendererPane.this,p);
  535.     }
  536.  
  537.         /**
  538.          * Returns whether this object can accept focus or not.
  539.          *
  540.          * @return true if object can accept focus; otherwise false
  541.          */
  542.         public boolean isFocusTraversable() {
  543.         return CellRendererPane.this.isFocusTraversable();
  544.     }
  545.  
  546.         /**
  547.          * Requests focus for this object.
  548.          */
  549.         public void requestFocus() {
  550.         CellRendererPane.this.requestFocus();
  551.         }
  552.  
  553.         /**
  554.          * Adds the specified focus listener to receive focus events from this 
  555.          * component. 
  556.          *
  557.          * @param l the focus listener
  558.          */
  559.         public void addFocusListener(FocusListener l) {
  560.         CellRendererPane.this.addFocusListener(l);
  561.     }
  562.  
  563.         /**
  564.          * Removes the specified focus listener so it no longer receives focus 
  565.          * events from this component.
  566.          *
  567.          * @param l the focus listener
  568.          */
  569.         public void removeFocusListener(FocusListener l) {
  570.         CellRendererPane.this.removeFocusListener(l);
  571.     }
  572.     } // inner class AccessibleCellRendererPane
  573. }
  574.  
  575.  
  576.